package com.lzx.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.answer.entity.Problem;
import com.lzx.answer.entity.Reply;
import com.lzx.answer.entity.SecondReply;
import com.lzx.answer.entity.vo.NicknameVO;
import com.lzx.answer.entity.vo.OssVO;
import com.lzx.answer.feign.UserClient;
import com.lzx.answer.mapper.ProblemMapper;
import com.lzx.answer.mapper.ReplyMapper;
import com.lzx.answer.mapper.SecondReplyMapper;
import com.lzx.answer.service.SecondReplyService;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-16
 */
@Service
public class SecondReplyServiceImpl extends ServiceImpl<SecondReplyMapper, SecondReply> implements SecondReplyService {
    @Resource
    private SecondReplyMapper secondReplyMapper;

    @Resource
    private ReplyMapper replyMapper;

    @Resource
    private ProblemMapper problemMapper;

    @Resource
    private UserClient userClient;

    //保存二级回复
    @GlobalTransactional
    @Override
    public R secondReply(SecondReply secondReply) {
        secondReply.setCreateTime(new Date());
        secondReplyMapper.insert(secondReply);

        //发送通知
        Inform inform = new Inform();
        if (secondReply.getSecondUserId() == -1) {
            Reply reply = replyMapper.selectById(secondReply.getFirstId());
            inform.setUserId(Math.toIntExact(reply.getUserId()));
        } else {
            inform.setUserId(secondReply.getSecondUserId());
        }
        inform.setSender(Math.toIntExact(secondReply.getSecondTwoUserId()));
        inform.setNickname(secondReply.getSecondTwoNickname());
        inform.setAvatar(secondReply.getSecondTwoAvatar());
        inform.setType(InformType.COMMENTPROBLEM.getType());
        Problem problem = problemMapper.selectById(secondReply.getProblemId());
        inform.setMessage(problem.getTitle());
        userClient.saveInform(inform);

        return R.ok().message("回复成功");
    }

    //根据问题id获取一级和二级回复
    @Override
    public R getFirstAndSecondReply(Long problemId) {
        //获取一级回复
        QueryWrapper<Reply> replyQueryWrapper = new QueryWrapper<>();
        replyQueryWrapper.eq("problem_id", problemId).orderByDesc("is_top").orderByDesc("like_count");
        List<Reply> firstReplyList = replyMapper.selectList(replyQueryWrapper);

        //获取二级回复的内容
        List<Reply> firstAndSecondReplyList = firstReplyList.stream().peek((firstReply) -> {
            QueryWrapper<SecondReply> secondReplyQueryWrapper = new QueryWrapper<>();
            secondReplyQueryWrapper.eq("first_id", firstReply.getId());
            List<SecondReply> secondReplyList = secondReplyMapper.selectList(secondReplyQueryWrapper);
            firstReply.setSecondReplyList(secondReplyList);
        }).collect(Collectors.toList());

        return R.ok().data("list", firstAndSecondReplyList);
    }

    //修改用户头像(问题二级回复)
    @Transactional
    @Override
    public String updateAvatarToSecondReply(OssVO ossVO) {
        QueryWrapper<SecondReply> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("second_user_id", ossVO.getId());
        List<SecondReply> secondReplyList = secondReplyMapper.selectList(wrapper1);
        for (SecondReply secondReply : secondReplyList) {
            secondReply.setSecondOneAvatar(ossVO.getAvatar());
            secondReplyMapper.updateById(secondReply);
        }
        QueryWrapper<SecondReply> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("second_two_user_id", ossVO.getId());
        List<SecondReply> list = secondReplyMapper.selectList(wrapper2);
        for (SecondReply secondReply : list) {
            secondReply.setSecondTwoAvatar(ossVO.getAvatar());
            secondReplyMapper.updateById(secondReply);
        }
        return "修改成功";
    }

    //修改用户昵称(问题二级回复)
    @Transactional
    @Override
    public String updateNicknameToSecondReply(NicknameVO nicknameVO) {
        QueryWrapper<SecondReply> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("second_user_id", nicknameVO.getId());
        List<SecondReply> secondReplyList = secondReplyMapper.selectList(wrapper1);
        for (SecondReply secondReply : secondReplyList) {
            secondReply.setSecondOneNickname(nicknameVO.getNickname());
            secondReplyMapper.updateById(secondReply);
        }
        QueryWrapper<SecondReply> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("second_two_user_id", nicknameVO.getId());
        List<SecondReply> list = secondReplyMapper.selectList(wrapper2);
        for (SecondReply secondReply : list) {
            secondReply.setSecondTwoNickname(nicknameVO.getNickname());
            secondReplyMapper.updateById(secondReply);
        }
        return "修改成功";
    }
}
